package com.example.lending.View;


import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lending.Model.Borrow;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.View.Adapter.UserAdapter;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllUserFragment extends Fragment {
    UserViewModel userViewModel;
    RecyclerView recyclerView;
    SharedPreferences sp;
    List<User>users;
    UserAdapter userAdapter;
    public AllUserFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userViewModel= ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication(),getActivity())).get(UserViewModel.class);
        recyclerView=getView().findViewById(R.id.rv_user);
        users=userViewModel.getAllUserLive().getValue();
        userAdapter=new UserAdapter(getContext(),users);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(userAdapter);
        userViewModel.getAllUserLive().observe(getActivity(), new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> users) {
                userAdapter.setAllUser(users);
                userAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_all_user, container, false);
    }

}
